<?php 
   
# do any validating for existing PL items for the current PL before coming here
# http://localhost:8888/CJLY/studio/_prefill_playlist_items.php?playlist_id=1908&pl_id_to_copy=1828

include "../includes/functions.php";
echo db_connect();

$playlist_id = $_POST['playlist_id']; if (!$playlist_id) $playlist_id = $_GET['playlist_id'];
$pl_id_to_copy = $_POST['pl_id_to_copy']; if (!$pl_id_to_copy) $pl_id_to_copy = $_GET['pl_id_to_copy'];
$show_id = $_POST['show_id'];

if (!$playlist_id || !$pl_id_to_copy || !$show_id) DIE("Missing one or more parameters.");

// echo "$playlist_id | $pl_id_to_copy | $show_id"; exit;

$sql = "SELECT * FROM playlist_items WHERE playlist_id = '$pl_id_to_copy'"; # echo "$sql<br><br>";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
	$ins1 = ""; $ins2=""; 
	foreach ($row as $key => $val)
	{     
		$ins1.= "$key,";
		if ($key == 'playlist_id') {
		   $ins2 .= "'$playlist_id',"; 
			} elseif ($key == 'time_played') {
		   	$ins2 .= "NOW(),";  
					} elseif ($key != 'playlist_item_id') {  
		   			$ins2.= "'".addslashes($val)."',";
						} else {
							$ins2.= "'',"; 
	  					}
	
	}
	 $insert = "INSERT INTO playlist_items (".rtrim($ins1,',').") VALUES (".rtrim($ins2,',').")";
		
		# echo "$insert<br>";
		if (!mysql_query($insert)) DIE ('Error saving copy of playlist items.');
	   # echo "DONE<br><br>";
		header ("location: playlist_new.php?show_id=$show_id&playlist_id=$playlist_id");
}

?>